﻿using System;
using System.Data;
using TopBuilds.Database;

namespace TopBuilds.Restaurant.Data
{
    public class BookServiceObject
    {
        public static BookService[] GetBookServices()
        {
            return Query(null);
        }

        public static BookService GetBookService(int id)
        {
            BookService bookService = new BookService();

            SqlBuilder builder = new SqlBuilder();
            builder.ConditionAdd(BookServiceDesc.Id, ConditionExpress.EQUALS, id);
            builder.QueryTableName = BookServiceDesc.TableName;
            string sql = builder.QueryCommandLine;

            DataTable bookServices = DatabaseObject.Instance.ExecuteQuerySQL(sql);

            bookService.Id                 = Convert.ToInt32(bookServices.Rows[0][BookServiceDesc.Id]);
            bookService.TableId            = Convert.ToInt32(bookServices.Rows[0][BookServiceDesc.TableId]);
            bookService.HeadCount          = Convert.ToInt32(bookServices.Rows[0][BookServiceDesc.HeadCount]);
            bookService.OrderTime          = Convert.ToDateTime(bookServices.Rows[0][BookServiceDesc.OrderTime]);
            bookService.BookTime           = Convert.ToDateTime(bookServices.Rows[0][BookServiceDesc.BookTime]);
            bookService.DeathTime          = Convert.ToDateTime(bookServices.Rows[0][BookServiceDesc.DeathTime]);
            bookService.RealComeTime       = Convert.ToDateTime(bookServices.Rows[0][BookServiceDesc.RealComeTime]);
            bookService.CustomerName       = Convert.ToString(bookServices.Rows[0][BookServiceDesc.CustomerName]);
            bookService.CustomerPhone      = Convert.ToString(bookServices.Rows[0][BookServiceDesc.CustomerPhone]);
            bookService.MemberId           = Convert.ToInt32(bookServices.Rows[0][BookServiceDesc.MemberId]);


            return bookService;
        }

        public static BookService[] Query(string condition)
        {
            // query factories from database
            string sql = "select * from " + BookServiceDesc.TableName;
            if (condition != null && condition.Length > 0)
            {
                sql += " where " + condition;
            }
            DataTable table = DatabaseObject.Instance.ExecuteQuerySQL(sql);


            // rebuild the data
            if (table != null && table.Rows.Count > 0)
            {
                BookService[] bookServices = new BookService[table.Rows.Count];

                for (int i = 0; i < table.Rows.Count; i++)
                {
                    BookService bookService = new BookService();

                    bookService.Id                 = Convert.ToInt32(table.Rows[i][BookServiceDesc.Id]);
                    bookService.TableId            = Convert.ToInt32(table.Rows[i][BookServiceDesc.TableId]);
                    bookService.HeadCount          = Convert.ToInt32(table.Rows[i][BookServiceDesc.HeadCount]);
                    bookService.OrderTime          = Convert.ToDateTime(table.Rows[i][BookServiceDesc.OrderTime]);
                    bookService.BookTime           = Convert.ToDateTime(table.Rows[i][BookServiceDesc.BookTime]);
                    bookService.DeathTime          = Convert.ToDateTime(table.Rows[i][BookServiceDesc.DeathTime]);
                    bookService.RealComeTime       = Convert.ToDateTime(table.Rows[i][BookServiceDesc.RealComeTime]);
                    bookService.CustomerName       = Convert.ToString(table.Rows[i][BookServiceDesc.CustomerName]);
                    bookService.CustomerPhone      = Convert.ToString(table.Rows[i][BookServiceDesc.CustomerPhone]);
                    bookService.MemberId           = Convert.ToInt32(table.Rows[i][BookServiceDesc.MemberId]);

                    bookServices.SetValue(bookService, i);
                }

                return bookServices;
            }
            return null;
        }

        public static Int32 Add(BookService bookService)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.InsertAdd(BookServiceDesc.TableId,           bookService.TableId);
            builder.InsertAdd(BookServiceDesc.HeadCount,         bookService.HeadCount);
            builder.InsertAdd(BookServiceDesc.OrderTime,         bookService.OrderTime);
            builder.InsertAdd(BookServiceDesc.BookTime,          bookService.BookTime);
            builder.InsertAdd(BookServiceDesc.DeathTime,         bookService.DeathTime);
            builder.InsertAdd(BookServiceDesc.RealComeTime,      bookService.RealComeTime);
            builder.InsertAdd(BookServiceDesc.CustomerName,      bookService.CustomerName);
            builder.InsertAdd(BookServiceDesc.CustomerPhone,     bookService.CustomerPhone);
            builder.InsertAdd(BookServiceDesc.MemberId,          bookService.MemberId);

            builder.QueryTableName = BookServiceDesc.TableName;

            string sql = builder.InsertCommandLine;

            return DatabaseObject.Instance.ExecuteNonQuerySQL(sql);
        }

        public static bool Delete(Int32 id)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.ConditionAdd(BookServiceDesc.Id, ConditionExpress.EQUALS, id);
            builder.QueryTableName = BookServiceDesc.TableName;
            string sql = builder.DeleteCommandLine;

            return (DatabaseObject.Instance.ExecuteNonQuerySQL(sql) != -1);
        }

        public static Int32 Update(BookService bookService)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.UpdateAdd(BookServiceDesc.TableId,          bookService.TableId);
            builder.UpdateAdd(BookServiceDesc.HeadCount,        bookService.HeadCount);
            builder.UpdateAdd(BookServiceDesc.OrderTime,        bookService.OrderTime);
            builder.UpdateAdd(BookServiceDesc.BookTime,         bookService.BookTime);
            builder.UpdateAdd(BookServiceDesc.DeathTime,        bookService.DeathTime);
            builder.UpdateAdd(BookServiceDesc.RealComeTime,     bookService.RealComeTime);
            builder.UpdateAdd(BookServiceDesc.CustomerName,     bookService.CustomerName);
            builder.UpdateAdd(BookServiceDesc.CustomerPhone,    bookService.CustomerPhone);
            builder.UpdateAdd(BookServiceDesc.MemberId, bookService.MemberId);

            builder.ConditionAdd(BookServiceDesc.Id, ConditionExpress.EQUALS, bookService.Id);
            builder.QueryTableName = BookServiceDesc.TableName;

            string sql = builder.UpdateCommandLine;

            return DatabaseObject.Instance.ExecuteNonQuerySQL(sql);
        }
    }
}
